package com.example.review.Fragment;


import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.review.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class PvFragment extends Fragment {
    @BindView(R.id.video)
    VideoView video;
    private View view;
    public PvFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_pv, container, false);
        String permissions1 = Manifest.permission.READ_EXTERNAL_STORAGE;
        String permissions2 = Manifest.permission.WRITE_EXTERNAL_STORAGE;
        mainfest(permissions1);
        mainfest(permissions2);
        ButterKnife.bind(this,view);
        Uri uri = copyFile("PV.mp4");
        MediaController mc = new MediaController(getContext());//Video是我类名，是你当前的类
        video.setMediaController(mc);
        video.setVideoURI(uri);
        video.start();
        return view;
    }
    private void mainfest(String permissions){
        int checkCallPhonePermission   = ContextCompat.checkSelfPermission(getContext(),permissions);
        if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
            //弹出对话框
            ActivityCompat.requestPermissions(getActivity(), new String[]{permissions}, 1);
        }
    }
    public Uri copyFile(String name) {
        try {
            File dir = getActivity().getFilesDir();
            File file = new File(dir, name);
            if (file.exists()) {
                Log.d("Test", "=========file exist=========");
                return Uri.fromFile(file);

            } else {
                file.createNewFile();
                OutputStream os = new FileOutputStream(file);
                InputStream is = getActivity().getAssets().open("Video/"+name);
                byte[] buffer = new byte[1024];
                int bufferRead = 0;
                while((bufferRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bufferRead);
                }
                os.flush();
                is.close();
                os.close();
                Log.d("Test", "=========copyFile success=========");
                return Uri.fromFile(file);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}


